﻿/*
 * 文件名称：
 * 程序员：yangsiyao
 * 创建时间：2008/6/2
 * 
 * 功能说明：系统类别管理页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：1) 增加快速添加类别, 修改页面提示功能 yhe 2009-09-11
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Config_CommonCategory : System.Web.UI.Page
{
    /// <summary>
    /// 当前类别的父级编号
    /// </summary>
    protected string TypeID
    {
        set { ViewState["TypeID"] = value; }
        get
        {
            return ViewState["TypeID"] != null ? (string)ViewState["TypeID"] : "0";
        }
    }


    protected string AllTypeID
    {
        set { ViewState["AllTypeID"] = value; }
        get
        {
            return ViewState["AllTypeID"] != null ?
                (string)ViewState["AllTypeID"] : "0";
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (Request.QueryString["ID"] != null)
        {
            TypeID = NetRequest.SafeRequest(Page, "ID", 1);

            AllTypeID = TypeID + "," + GetAllTypeID(TypeID).Trim(',').Trim(',');
        }

        CreateTree();

        if (Request.QueryString["op"] != null)
        {
            //showhint
            string op = Request.QueryString["op"].ToString().ToLower();
            string msg = "";
            if (op == "")
            {
                return;
            }
            else if (op == "add")
            {
                msg = "增加栏目成功!";
            }
            else if (op == "modify")
            {
                msg = "修改栏目成功!";
            }
            else if (op == "delete")
            {
                msg = "删除栏目成功!";
            }
            string script = string.Format("<script>showhint('0','{0}')</script>", msg);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "init", script);
        }
    }

    /// <summary>
    /// 创建一级栏目
    /// </summary>
    private void CreateTree()
    {
        string TreeTableString = "";    //输出字符串

        string parentID = TypeID;//父级编号   //"0";

        string strSQL = string.Format("SELECT CategoryId,CategoryName,ParentId FROM Whir_CommonCategory WHERE ParentId={0} Order by ParentId,sort", parentID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        if (dt.Rows.Count > 0)
        {
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                string imgTr = "";  //显示图片的信息
                if (dt.Rows.Count == 1) //只有一行数据，不进行操作
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" /></td>";
                }
                else if (k == 0)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','down','" + dt.Rows[k + 1]["CategoryId"].ToString() + "')\" /></td>";
                }
                else if (k == dt.Rows.Count - 1)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','up','" + dt.Rows[k - 1]["CategoryId"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" disabled=\"disabled\" /></td>";
                }
                else
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','up','" + dt.Rows[k - 1]["CategoryId"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','down','" + dt.Rows[k + 1]["CategoryId"].ToString() + "')\" /></td>";
                }

                TreeTableString += "<tr id=\"node-" + dt.Rows[k]["CategoryId"].ToString() + "\">";
                //TreeTableString += "<td><span class=\"folder\">" + dt.Rows[k]["CategoryName"].ToString() + "</span></td>";
                TreeTableString += "<td><span class=\"folder\" onclick='$(\"#node-" + dt.Rows[k]["CategoryId"].ToString() + "\").toggleBranch();'>" + dt.Rows[k]["CategoryName"].ToString() + "(" + dt.Rows[k]["CategoryId"].ToString() + ")</span></td>";
                TreeTableString += "<td></td>";
                TreeTableString += imgTr;
                TreeTableString += "<td><a href=\"AddType.aspx?Action=Modify&ColumnID=" + dt.Rows[k]["CategoryId"].ToString() + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=550&modal=true\" class=\"thickbox\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:ConFirm('" + dt.Rows[k]["CategoryId"].ToString() + "')\">删除</a></td>";
                TreeTableString += "</tr>";
                TreeTableString += CreateSubTreeNode(dt.Rows[k]["CategoryId"].ToString());
            }
        }
        else
        {
            TreeTableString += "<tr>";
            TreeTableString += "<td colspan='4' align='center'>暂无记录!!</td>";
            TreeTableString += "</tr>";
        }

        Literal1.Text = TreeTableString;
    }


    /// <summary>
    /// 创建子栏目
    /// </summary>
    /// <param name="columnID"></param>
    /// <returns></returns>
    private string CreateSubTreeNode(string CategoryId)
    {
        string SubNodeString = "";

        string strSQL = string.Format("SELECT CategoryId,CategoryName,ParentId FROM Whir_CommonCategory WHERE ParentID={0} Order by ParentId,sort", CategoryId);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        if (dt.Rows.Count > 0)
        {
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                string imgTr = "";
                if (dt.Rows.Count == 1) //只有一行数据，不进行操作
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" /></td>";
                }
                else if (k == 0)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','down','" + dt.Rows[k + 1]["CategoryId"].ToString() + "')\" /></td>";
                }
                else if (k == dt.Rows.Count - 1)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','up','" + dt.Rows[k - 1]["CategoryId"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" disabled=\"disabled\" /></td>";
                }
                else
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','up','" + dt.Rows[k - 1]["CategoryId"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["CategoryId"].ToString() + "','down','" + dt.Rows[k + 1]["CategoryId"].ToString() + "')\" /></td>";
                }

                SubNodeString += "<tr id=\"node-" + dt.Rows[k]["CategoryId"].ToString() + "\" class=\"child-of-node-" + dt.Rows[k]["parentId"].ToString() + "\">";
                //SubNodeString += "<td><span class=\"folder\">" + dt.Rows[k]["CategoryName"].ToString() + "</span></td>";
                SubNodeString += "<td><span class=\"folder\" onclick='$(\"#node-" + dt.Rows[k]["CategoryId"].ToString() + "\").toggleBranch();'>" + dt.Rows[k]["CategoryName"].ToString() + "(" + dt.Rows[k]["CategoryId"].ToString() + ")</span></td>";
                SubNodeString += "<td></td>";
                SubNodeString += imgTr;
                SubNodeString += "<td><a href=\"AddType.aspx?Action=Modify&ColumnID=" + dt.Rows[k]["CategoryId"].ToString() + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=550&modal=true\" class=\"thickbox\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:ConFirm('" + dt.Rows[k]["CategoryId"].ToString() + "')\">删除</a></td>";
                SubNodeString += "</tr>";
                SubNodeString += CreateSubTreeNode(dt.Rows[k]["CategoryId"].ToString());
            }
        }

        return SubNodeString;
    }


    private string GetAllTypeID(string CategoryId)
    {
        string strResult = "";
        if (CategoryId == "") return strResult;

        string strSQL = "SELECT parentID FROM Whir_CommonCategory WHERE parentID != 0 AND CategoryId = " + CategoryId;
        object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSQL);

        try
        {
            if (obj != null)
            {
                strResult += obj.ToString() + ",";
                strResult += GetAllTypeID(obj.ToString());
            }
        }
        catch { }

        return strResult;
    }
}
